package vn.lumi.smarthome.network;

import java.util.ArrayList;
import java.util.Arrays;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import vn.lumi.smarthome.provider.SmartHomeProvider;
import vn.lumi.smarthome.io.database.DatabaseHelper;
import vn.lumi.smarthome.io.database.TBL_Device;
import vn.lumi.smarthome.io.database.TBL_DeviceScene;
import vn.lumi.smarthome.io.database.TBL_DeviceState;
import vn.lumi.smarthome.io.database.TBL_Scene;
import vn.lumi.smarthome.io.model.Device;
import vn.lumi.smarthome.io.model.Scene;

public class UpdateDeviceStatus extends HCMsg {

	// Attributes
	private ArrayList<Device> Devices = null;
	private boolean isDeviceValid = true;

	public UpdateDeviceStatus(Context context) {
		super(NetMessage.CMD_HC_UPDATE_DEVICE_STATUS, 0);
		Devices = new ArrayList<Device>();
		super.mContext = context;
	}

	@Override
	public void parseMessage(byte[] input, int length) {
		if (isCorrectFormat(input, length) == true) {
			int currentPos = 1;
			while (currentPos < length) {
				Device dev = new Device();
				dev.setId(Byte2Unsigned(input[currentPos]));
				currentPos++;

				// must get this value from database!!!!!!!!!!!!!!!!!!!!!!!
				int stateLength = SmartHomeProvider.getStateLengthByID(
						this.mContext, dev.getId());
				if (stateLength < 0) {
					isDeviceValid = false;
					// Hieu sua
					//return;
					currentPos++;
				} else {
					int[] state = new int[stateLength];
					for (int k = 0; k < stateLength; k++) {
						state[k] = Byte2Unsigned(input[currentPos]);
						currentPos++;
					}
					dev.setDeviceState(SmartHomeProvider.getDeviceStateByID(
							this.mContext, dev.getId(), state));
					System.out.println("Dev id=" + dev.getId() + " state="
							+ Arrays.toString(state));
					Devices.add(dev);
				}
			}
		}
	}

	public void setContext(Context context) {
		mContext = context;
	}

	@Override
	public void doWhenReceived(Context context) {
		// TODO Auto-generated method stub
		// Hieu sua
//		if (isDeviceValid == false)
//			return;
		// end
		for (Device device : Devices) {
			System.out.println("Dev id=" + device.getId());
			}
		for (Device device : Devices) {
			
			int devStateID = SmartHomeProvider.getDeviceStateIDByDeviceID(
					context, device.getId());
			ContentValues values = new ContentValues();
			// values.put(TBL_DeviceState.ID, devStateID);
			values.put(TBL_DeviceState.BLIND_STATE, device.getDeviceState()
					.getBlind_state());
			values.put(TBL_DeviceState.RGB, device.getDeviceState().getRGB());
			values.put(TBL_DeviceState.VALUE, device.getDeviceState()
					.getValue());
			String selection = TBL_DeviceState.ID + "=?";
			String[] selectionArgs = new String[] { String.valueOf(devStateID) };
			context.getContentResolver().update(
					TBL_DeviceState.CONTENT_URI_DEVICE_STATE, values,
					selection, selectionArgs);

			// find roomid of device
			String[] projection = new String[] { TBL_Device.ROOM_ID,
					TBL_Device.TYPE_ID };
			selection = new String(DatabaseHelper.DB_TBL_DEVICE + "."
					+ TBL_Device._ID + "=?");
			selectionArgs = new String[] { String.valueOf(device.getId()) };
			Cursor cursor = context.getContentResolver().query(
					TBL_Device.CONTENT_URI_DEVICES, projection, selection,
					selectionArgs, null);
			if (cursor == null)
				return;
			cursor.moveToFirst();
			int roomId = cursor.getInt(cursor
					.getColumnIndex(TBL_Device.ROOM_ID));
			int deviceType = cursor.getInt(cursor
					.getColumnIndex(TBL_Device.TYPE_ID));

			// find turned on scene in room
			// Hieu sua
//			projection = new String[] { TBL_Scene._ID };
//			selection = new String(TBL_Scene.ROOM_ID + "=? AND "
//					+ TBL_Scene.STATE + "=?");
//			selectionArgs = new String[] { String.valueOf(roomId),
//					String.valueOf(Scene.SCENE_STATE_ON) };
//			cursor = context.getContentResolver().query(
//					TBL_Scene.CONTENT_URI_SCENE, projection, selection,
//					selectionArgs, null);
//			if (cursor.getCount() != 1)
//				return;
//			cursor.moveToFirst();
//			int turnedOnSceneId = cursor.getInt(cursor
//					.getColumnIndex(TBL_Scene._ID));
//			
//			
//			// find device state in scene
//			projection = new String[] { TBL_DeviceScene.STATE_ID };
//			selection = new String(TBL_DeviceScene.DEVICE_ID + "=? AND "
//					+ TBL_DeviceScene.SCENE_ID + "=?");
//			selectionArgs = new String[] { String.valueOf(device.getId()),
//					String.valueOf(turnedOnSceneId) };
//			cursor = context.getContentResolver().query(
//					TBL_DeviceScene.CONTENT_URI_DEVICE_SCENE, projection,
//					selection, selectionArgs, null);
//			if (cursor.getCount() == 0)
//				return;
//
//			int stateId = cursor.getInt(cursor
//					.getColumnIndex(TBL_DeviceScene.STATE_ID));
//			selection = new String(TBL_DeviceState._ID + "=?");
//			selectionArgs = new String[] { stateId + "" };
//			cursor = context.getContentResolver().query(
//					TBL_DeviceState.CONTENT_URI_DEVICE_STATE, null, selection,
//					selectionArgs, null);
//			if (cursor == null)
//				return;
//			cursor.moveToFirst();
//			int value = cursor.getInt(cursor
//					.getColumnIndex(TBL_DeviceState.VALUE));
//			int RGB = cursor.getInt(cursor.getColumnIndex(TBL_DeviceState.RGB));
//
//			// if device state is changed --> turn off scene
//			boolean isSceneChanged = false;
//			switch (deviceType) {
//			case Device.DEVICE_TYPE_RGB:
//				if (device.getDeviceState().getRGB() != RGB)
//					isSceneChanged = true;
//				break;
//			default:
//				if (device.getDeviceState().getValue() != value)
//					isSceneChanged = true;
//				break;
//			}
//			if (isSceneChanged == true) {
//				selection = new String(TBL_Scene._ID + "=?");
//				selectionArgs = new String[] { turnedOnSceneId + "" };
//				values.clear();
//				values.put(TBL_Scene.STATE, Scene.SCENE_STATE_OFF);
//				context.getContentResolver().update(
//						TBL_Scene.CONTENT_URI_SCENE, values, selection,
//						selectionArgs);
//			}
		}
	}
}
